##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)
library(patchwork)
library(tidyverse)


files = paste0('results/',c('current-results-2012-2016-Asian-subset.Rdata',
                                        'current-results-2016-2020-Asian-subset.Rdata',
                                        'current-results-2012-2016-Black-subset.Rdata',
                                        'current-results-2016-2020-Black-subset.Rdata',
                                        'current-results-2012-2016-Hispanic-subset.Rdata',
                                        'current-results-2016-2020-Hispanic-subset.Rdata',
                                        'current-results-2012-2016-White-subset.Rdata',
                                        'current-results-2016-2020-White-subset.Rdata'
                                        ))




results = rbindlist(lapply(files, FUN=function(file){

  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
   # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }


return(out)

  }))
  
  l[grepl('2008',file),Year1:='2008']
  l[grepl('2008',file),Year2:='2012']
l[grepl('2020',file),Year1:='2016']
l[grepl('2020',file),Year2:='2020'] 
l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
l[,file:=file]

return(l)
}))


results = results[Covariate %in% c('InteractionWhite:DemSpExpDiff_nohh','InteractionWhite:RepSpExpDiff_nohh',
                                   'InteractionNon-White:DemSpExpDiff_nohh','InteractionNon-White:RepSpExpDiff_nohh',
                                   
                                   'InteractionBlack:DemSpExpDiff_nohh','InteractionBlack:RepSpExpDiff_nohh',
                                   'InteractionNon-Black:DemSpExpDiff_nohh','InteractionNon-Black:RepSpExpDiff_nohh',
                                   
                                   
                                   'InteractionHispanic:DemSpExpDiff_nohh','InteractionHispanic:RepSpExpDiff_nohh',
                                   'InteractionNon-Hispanic:DemSpExpDiff_nohh','InteractionNon-Hispanic:RepSpExpDiff_nohh',
                                   
                                   
                                   'InteractionAsian:DemSpExpDiff_nohh','InteractionAsian:RepSpExpDiff_nohh',
                                   'InteractionNon-Asian:DemSpExpDiff_nohh','InteractionNon-Asian:RepSpExpDiff_nohh'
                                   )]

results[!grepl('Non-',Covariate),Type:='Ingroup']
results[grepl('Non-',Covariate),Type:='Outgroup']

results[grepl('White',file),Race:='White']
results[grepl('Asian',file),Race:='Asian']
results[grepl('Black',file),Race:='Black']
results[grepl('Hispanic',file),Race:='Hispanic']

#results[grepl('DemSpExp',Model),Exposure.Type:='Exposure to Democrats\non Democratic registration']
#results[grepl('RepSpExp',Model),Exposure.Type:='Exposure to Republicans\non Republican registration']
results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']
results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']


results[grepl('Dems',Model),Subset:='Democrats']
results[grepl('Reps',Model),Subset:='Republicans']
results[grepl('Oths',Model),Subset:='Non-partisans']
results[,Years:=paste0(Year1,'-',Year2)]

colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")

g = ggplot(results[Years=='2016-2020'] %>%
             mutate(Race = paste0(Race,' partisan exposure')), aes(y = Estimate, x = Subset, color = Subset, shape = Type))+
  geom_point( size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Effect of partisan exposure')+
  xlab('Original party')+
  guides(color = 'none')+
  theme(text = element_text(size = 15, family = 'serif'),panel.spacing = unit(.5, "lines"), strip.text.y = element_text(size = 10, angle = 0), legend.title=element_blank(),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Race, scales = 'free_x')
ggsave(plot = g,filename = 'figures/Fig7.png',dpi=300, width = 13, height = 5, units = 'in')


#2012-2016


g = ggplot(results[Years=='2012-2016'] %>%
             mutate(Race = paste0(Race,' partisan exposure')), aes(y = Estimate, x = Subset, color = Subset, shape = Type))+
  geom_point( size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Effect of partisan exposure')+
  xlab('Original party')+
  guides(color = 'none')+
  theme(text = element_text(size = 15, family = 'serif'),panel.spacing = unit(.5, "lines"), strip.text.y = element_text(size = 10, angle = 0), legend.title=element_blank(),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Race, scales = 'free_x')
ggsave(plot = g,filename = 'figures/FigS6.png',dpi=300, width = 13, height = 5, units = 'in')





